/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing;

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 *
 * @author Francisco Morero Peyrona
 */

public final class JActionSet implements ActionListener
{
   public static final Object SEPARATOR = null;

   // The listener to action events (usually the main UI)
   private EventListenerList listeners;
   // Set of actions
   private Vector vActions = null;

   //-------------------------------------------------------------------------//

   public JActionSet()
   {
      this.vActions = new Vector();
   }

   /**
    * Adds a new action.
    * 
    * @param icon
    * @param sName
    * @param sToolTip
    * @param nMnemonic One of the constants defined in <code>KeyEvent</code> class 
    * @param sActionCommand
    * 
    * @return
    */
   public AbstractAction add( ImageIcon icon, String sName, String sToolTip, String sActionCommand )
   {
      JAction action = new JAction( icon, sName, sToolTip, sActionCommand );

      this.vActions.add( action );

      return action;
   }

   /**
    * Adds a separator
    */
   public void addSeparator()
   {
      this.vActions.add( JActionSet.SEPARATOR );
   }

   /**
    *
    * @return
    */
   public Enumeration actions()
   {
      return this.vActions.elements();
   }

   /**
    *
    * @return
    */
   public JAction[] getActions()
   {
      JAction[] aRet = new JAction[ this.vActions.size() ];
      vActions.copyInto( aRet );

      return aRet;
   }
   
   /**
    * 
    * @param sName
    * @return
    */
   public JAction getActionByName( String sName )
   {
      sName = sName.trim();
      
      for( int n = 0; n < this.vActions.size(); n++ )
      {
         JAction action = (JAction) this.vActions.get( n ); 
      
         if( action != null && action.getValue( Action.NAME ).equals( sName ) )
            return (JAction) this.vActions.get( n );
      }
      
      return null; 
   } 

   /**
    * 
    * @param sActionCommand
    * @return
    */
   public JAction getActionByCommand( String sActionCommand )
   {
      for( int n = 0; n < this.vActions.size(); n++ )
      {
         JAction action = (JAction) this.vActions.get( n );
      
         if( action != null && action.getValue( Action.ACTION_COMMAND_KEY ).equals( sActionCommand ) )
            return (JAction) this.vActions.get( n );
      }
      
      return null; 
   } 

   /**
    * Forwards the ActionEvent to the registered listener.
    */
   public void actionPerformed( ActionEvent evt )
   {
      if( listeners != null )
      {
         Object[] listenerList = listeners.getListenerList();
         JAction  action       = (JAction) ((AbstractButton) evt.getSource()).getAction();

         // Recreate the ActionEvent
         ActionEvent e = new ActionEvent( evt.getSource(), evt.getID(), (String) action.getValue( Action.ACTION_COMMAND_KEY ) );

         for( int n = 0; n <= listenerList.length - 2; n += 2 )
            ((ActionListener) listenerList[n + 1]).actionPerformed( e );
      }
   }

   /**
    *
    * @param l
    */
   public void addActionListener( ActionListener l )
   {
      if( listeners == null )
         listeners = new EventListenerList();

      listeners.add( ActionListener.class, l );
   }

   /**
    *
    * @param l
    */
   public void removeActionListener( ActionListener l )
   {
      if( listeners == null )
         return;

      listeners.remove( ActionListener.class, l );
   }

   //-------------------------------------------------------------------------//
   // INNER CLASS: JAction
   //-------------------------------------------------------------------------//

   public final class JAction extends AbstractAction
   {
      /**
       * Constructor
       *
       * @param icon
       * @param sName
       * @param sToolTip
       * @param cMnemonic
       * @param sActionCommand
       */
      public JAction( ImageIcon icon, String sName, String sToolTip, String sActionCommand )
      {
         String sAccStr = "";
         
         putValue( Action.NAME              , sName.trim() );
         putValue( Action.SMALL_ICON        , icon );
         putValue( Action.SHORT_DESCRIPTION , sToolTip.trim() + sAccStr );
         putValue( Action.LONG_DESCRIPTION  , sToolTip.trim() );
         putValue( Action.ACTION_COMMAND_KEY, sActionCommand );
      }

      /**
       * Calls JActionSet.actionPerformed method
       * @param evt
       */
      public void actionPerformed( ActionEvent evt )
      {
         JActionSet.this.actionPerformed( evt );
      }
   }
}
